#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''GUI for http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

   very simple, just adjust the values to your terminal and run the script'''

import time
import os

from cellular_automata.game_of_life import GameOfLife
from cellular_automata.cellular_engine.constants import WORLD_TYPE_TORUS

WORLD_SIZE = (12, 12)
TILE_SIZE = (1, 1)

gol = GameOfLife(WORLD_SIZE, TILE_SIZE, WORLD_TYPE_TORUS)
gol.new_seed(0.5)

CELL_ALIVE = '1'
CELL_DEAD = '0'
WALL = '#'

DELAY = 0.05


# http://stackoverflow.com/a/684344/940789 - this is just so wrong :D
def cls():
    os.system(['clear', 'cls'][os.name == 'nt'])

if __name__ == '__main__':
    height, width = WORLD_SIZE
    filler = WALL * (width + 2)
    
    while True:
        gol.game_loop()
        alive_cells = gol.alive_cells

        print(alive_cells)
        raw_input()
        
        cls()
        
        things_to_print = []

        things_to_print.append(filler + '\n')
        for y in range(height):
            things_to_print.append(WALL)
            for x in range(width):
                if (x, y) in alive_cells:
                    things_to_print.append(CELL_ALIVE)
                else:
                    things_to_print.append(CELL_DEAD)
            things_to_print.append(WALL + '\n')
        things_to_print.append(filler)

        print(''.join(things_to_print))

        time.sleep(DELAY)
